package com.xx.secret.util;

import cn.hutool.core.util.ArrayUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

/**
 * @author ：wangcheng
 * @date 2023/12/20
 **/
@Slf4j
public class JsonUtil {

    private static final ObjectMapper objectMapper = new ObjectMapper();

    public static JsonNode getValue(String jsonStr, String keyPath)  {
        JsonNode jsonNode = null;
        try {
            jsonNode = objectMapper.readTree(jsonStr);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        String[] paths = keyPath.split("\\.");
        JsonNode currentNode = jsonNode;
        for (String key : paths) {
            currentNode = currentNode.get(key);
            if (currentNode == null) {
                return null;
            }
        }
        return currentNode;
    }


    public static  <T> T getValue(String jsonStr, String keyPath, Class<T> cls)  {
        JsonNode jsonNode = getValue(jsonStr, keyPath);
        if (jsonNode == null) {
            return null;
        }
        try {
            return objectMapper.treeToValue(jsonNode, cls);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    public static String removeKey(String jsonStr,String[] keyPath) {
        if (ArrayUtil.isEmpty(keyPath) || StringUtils.isBlank(jsonStr)) {
            return jsonStr;
        }
        try {
            JsonNode jsonNode = objectMapper.readTree(jsonStr);
            for (String s : keyPath) {
                String[] paths = s.split("\\.");
                JsonNode currentNode = jsonNode;
                for (int i = 0; i < paths.length; i++) {
                    int size = paths.length;
                    String path = paths[i];
                    if (size == 1) {
                        JsonNode remove = ((ObjectNode) currentNode).remove(path);
                    }else {
                        currentNode = currentNode.get(paths[i]);
                        if (currentNode == null) {
                            log.info("删除的节点中有null值 节点路径: {} 当前节点路径: {} ",keyPath,paths[i]);
                            return jsonStr;
                        }
                        if (currentNode !=null&& i == size -2 ) {
                            JsonNode remove = ((ObjectNode) currentNode).remove(paths[i+1]);
                            break;
                        }
                    }
                }
            }
            return jsonNode.toString();
        }catch (Exception e) {
            log.error("移除key异常: {} ",e.getMessage(),e);
        }
        return jsonStr;
    }

    public static void main(String[] args) throws JsonProcessingException {
        String json2 = "{\n" +
                "\t\"platformUserId\": \"11\",\n" +
                "\t\"loanInfo\": {\n" +
                "\t\t\"platformCreditNo\": \"\",\n" +
                "\t\t\"imageData\": \"saferrfdasfa\",\n" +
                "\t\t\"regisPhoneNo\": \"s\"\n" +
                "\t}\n" +
                "}";
        String ss = removeKey(json2, new String[]{"loanInfo.imageData"});
        log.info("xx:{}",ss);
    }

}
